package my.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class StringUtil {
	//获取最长子串
	public static String getCommonStrLength(String str1, String str2) {
		str1 = str1.toLowerCase();
		str2 = str2.toLowerCase();
		int len1 = str1.length();
		int len2 = str2.length();
		String min = null;
		String max = null;
		String target = null;
		min = len1 <= len2 ? str1 : str2;
		max = len1 > len2 ? str1 : str2;
		// 最外层：min子串的长度，从最大长度开始
		for (int i = min.length(); i >= 1; i--) {
			// 遍历长度为i的min子串，从0开始
			for (int j = 0; j <= min.length() - i; j++) {
				target = min.substring(j, j + i);
				// 遍历长度为i的max子串，判断是否与target子串相同，从0开始
				for (int k = 0; k <= max.length() - i; k++) {
					if (max.substring(k, k + i).equals(target)) {
						return target;
					}
				}
			}
		}
		return null;
	}
	//字符串是否为空
	public static boolean isNULL(Object str){
		return str==null||"".equals(str)||"null".equals(str.toString().toLowerCase())||"\"null\"".equals(str.toString().toLowerCase());
	}
	/**
	 * 判断经纬度为null，或0.0000000
	 */
	public static boolean  isNullByLatitude(Double latitude){
		return latitude==null||latitude==0;
	}
	
	/**
	 * 将数字保留一位小数
	 * @param number
	 * @return
	 */
	public static String numberFormat(String number){
		DecimalFormat df=new DecimalFormat(".##");
		double d=Double.parseDouble(number);
		String st=df.format(d);
		return st;
	}
	
	public static String extractValue(String string,Collection<String> c){
		String result = null;
		for (String string2 : c) {
			//string2参数不为空，且string中包含string2
			if(!StringUtil.isNULL(string2)&&string.contains(string2)){
				if(result==null){
					result = string2;
				}else{
					if(string2.length()>result.length()){
						result = string2;
					}
				}
			}
		}
		return result;
	}
	
	/**
	 * 提取原话中的参数
	 * @param content 原话
	 * @param c 参数集
	 * @return
	 */
	public static Set<String> extractValues(String content,Collection<String> c){
		List<StringIndex> stringIndexs = new ArrayList<StringIndex>();
		//抽出所有能匹配的关键词
		for (String key : c) {
			valueIndex(content, key, stringIndexs);
		}
		/*
		 * 对关键词进行去重分析：
		 * 1. 关键词完全一样
		 * 2. 关键词不一样，但关键词是包含关系
		 * （“台灯”关键词包含“灯”关键词）
		 */
		StringIndex[] chars = new StringIndex[content.length()];
		for (StringIndex stringIndex : stringIndexs) {
			putStringIndex(stringIndex, chars);
		}
		Set<String> result = new HashSet<String>();
		for (StringIndex stringIndex : chars) {
			if(stringIndex!=null){
				result.add(stringIndex.getValue());
			}
		}
		return result;
	}
	/**
	 * 获取content中key所在字符区间（起始index、结尾index）
	 * @param content
	 * @param key
	 * @return
	 */
	public static void valueIndex(String content,String key,List<StringIndex> stringIndexs){
		StringIndex stringIndex = null;
		if(StringUtil.isNULL(content)||StringUtil.isNULL(key))
			return ;
		if(content.contains(key)){
			List<Integer> stringIndex2 = getStringIndex(content, key);
			for (Integer integer : stringIndex2) {
				int end = integer + key.length()-1;
				stringIndex = new StringIndex(key, integer, end);
				stringIndexs.add(stringIndex);
			}
		}
	}
	/**
	 * 将关键词放入关键词数组中
	 * @param stringIndex
	 * @param chars
	 */
	public static void putStringIndex(StringIndex stringIndex,StringIndex[] chars){
		int start = stringIndex.getStart();
		int end = stringIndex.getEnd();
//		String value = stringIndex.getValue();
		for (int i = start; i <= end; i++) {
			boolean put = true;
			if(chars[i]!=null){
				StringIndex old = chars[i];
				int oldStart = old.getStart();
				int oldEnd = old.getEnd();
				if(start>=oldStart&&end<=oldEnd){//新的关键词被旧的关键词包含
					put = false;
				}
			}
			if(put){
				chars[i] = stringIndex;
			}
		}
	}
	/**
	 * 获取字符串中关键词所有位置
	 * @param content
	 * @param key
	 * @return
	 */
	public static List<Integer> getStringIndex(String content,String key){
		List<Integer> result = new ArrayList<Integer>();
		int beforeIndex = 0;
		while(true){
			int indexOf = content.indexOf(key, beforeIndex);
			if(indexOf==-1)
				break;
			result.add(indexOf);
			beforeIndex = indexOf + 1;
		}
		return result;
	}
	
}
class StringIndex {
	private String value;
	private int start;
	private int end;
	public StringIndex(String value, int start, int end) {
		this.value = value;
		this.start = start;
		this.end = end;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	
}

